package mvc.controller;

import mvc.model.pojos.ProductBean;
import mvc.model.pojos.RomBean;
import mvc.model.service.*;
import mvc.model.service.impl.*;
import mvc.model.vo.Color_Path;
import mvc.model.vo.Setmeal;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "DetailServlet",urlPatterns = "maindetail")
public class DetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pname = req.getParameter("pname");
        //根据名字查pid
        ProductService productService = new ProductServiceImpl();
        P_infoService p_infoService = new P_infoServiceImpl();
        List<ProductBean> pro = productService.queryByName(pname);
        RomService romService = new RomServiceImpl();
        ColorService colorService = new ColorServiceImpl();
        CPUService cpuService = new CPUServiceImpl();
        Map<Integer,String> roms = new HashMap<>();
        Map<Integer, Color_Path> colors = new HashMap<>();
        Map<Integer,Integer> cpus = new HashMap<>();
        Map<Integer,Integer> flags = new HashMap<>();
        //根据pid循环查各个的属性
        for (int i = 0;i < pro.size();i++){
            int number = pro.get(i).getNumber();
            //查颜色各个id
            int rid = p_infoService.queryID(number).getRid();
            int cid = p_infoService.queryID(number).getCid();
            int sid = p_infoService.queryID(number).getSid();
            int online = p_infoService.queryID(number).getOnline();
            int cpuid = p_infoService.queryID(number).getCpuid();
            int flag = p_infoService.queryID(number).getFlag();
            //查询各个id将各个属性分别放进各个map里
            //内存
            RomBean romBean = romService.query(rid);
            String rom = romBean.getRsize()+"+"+romBean.getMsize()+"G";
            roms.put(rid,rom);
            //颜色和图片
            Color_Path color_path = p_infoService.queryPath(pro.get(i).getPid(),cid);
            colors.put(cid,color_path);
        }
        //先查出这个手机默认的第一个的套餐情况，后面相同手机的不同款式套餐用另外的servlet
        List<Setmeal> setMeal = p_infoService.queryMeal(pro.get(0).getPid());
        req.getSession().setAttribute("roms",roms);
        req.getSession().setAttribute("colors",colors);
        req.getSession().setAttribute("setMeal",setMeal);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
